package com.qf.admin.controller;


import com.qf.admin.entity.User;
import com.qf.admin.model.Results;
import com.qf.admin.pojo.QueryUser;
import com.qf.admin.service.IUserService2;
import com.qf.admin.model.TableData;


import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "*")
public class UserController {

    private IUserService2 userService;

    public UserController(IUserService2 userServiceImpl2) {
        this.userService = userServiceImpl2;
    }

    @GetMapping
    public TableData<User> getPageData(QueryUser queryUser , Integer offset, Integer limit){

        TableData<User> pageData = userService.getPageData(queryUser, offset, limit);

        return pageData;

    }

    //禁用用户
    @GetMapping("/{id}")
    public Results disableUser(@PathVariable("id") Integer id){
        Results results = new Results();
        try {
        userService.disableUser(id);
            results.setCode(1);
            results.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            results.setCode(-1);
            results.setMsg("fail");
        }

        return results;
    }

    @PutMapping("/{id}")
    public Results activeUser(@PathVariable("id") Integer id){
        Results results = new Results();
        try {
            userService.activeUser(id);
            results.setCode(1);
            results.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            results.setCode(-1);
            results.setMsg("fail");
        }

        return results;
    }
}
